<template>
  <div>
    <CategorySelector
      @changeCategory="changeCategory"
      :disabled="showList !== 1"
    />
    <el-card style="margin-top: 10px">
      <template v-if="showList === 1">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="medium"
          style="margin-top: 10px"
          @click="addSpuList"
          :disabled="!category3Id"
        >
          添加SPU
        </el-button>
        <el-table :data="SpuList" border style="width: 100%; margin: 20px 0">
          <el-table-column label="序号" type="index" width="80" align="center">
          </el-table-column>
          <el-table-column
            prop="spuName"
            label="SPU名称"
            width="130"
            align="center"
          >
          </el-table-column>
          <el-table-column label="SPU描述" align="center" prop="description">
          </el-table-column>
          <el-table-column label="操作" width="250" align="center">
            <template slot-scope="{ row }">
              <el-button
                icon="el-icon-circle-plus"
                size="mini"
                @click="addSkuList(row)"
                style="color: orange"
              >
              </el-button>
              <el-button
                icon="el-icon-edit"
                size="mini"
                @click="UpdateSpu(row)"
                style="color: blue"
              >
              </el-button>
              <el-button icon="el-icon-more" @click="moveSku(row)" size="mini" style="color: pink">
              </el-button>
              <el-button
                icon="el-icon-delete"
                size="mini"
                @click="removeSpu(row)"
                style="color: red"
              >
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[3, 6, 9, 12]"
          :page-size="limit"
          layout="->,prev, pager, next, jumper, -> , sizes, total"
          :total="total"
        >
        </el-pagination>
      </template>
      <AddOrUpdateSpu
        :setSpuList="setSpuList"
        :category3Id="category3Id"
        @showPage="showPage"
        v-if="showList === 2"
      />
      <SaveSku
        :setSpuList="setSpuList"
        :category1Id="category1Id"
        :category2Id="category2Id"
        :category3Id="category3Id"
        v-if="showList === 3"
        @showPage="showPage"
      />
      <el-dialog
        title="SKU列表"
        :visible.sync="dialogVisible"
        width="70%"
      >
       <el-table :data="SkuList" border style="width: 100%; margin: 20px 0" v-loading="!SkuList.length">
      <el-table-column label="序号" type="index" width="80" align="center">
      </el-table-column>
      <el-table-column
        prop="skuName"
        label="SKU名称"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column label="SKU描述" align="center" prop="skuDesc">
      </el-table-column>
      <el-table-column label="SKU价格" align="center" prop="price">
      </el-table-column>
      <el-table-column label="SKU重量" align="center" prop="weight">
      </el-table-column>
      <el-table-column label="默认图片" align="center">
        <template slot-scope="{ row }">
          <img
            :src="row.skuDefaultImg"
            :alt="row.skuName"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
    </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import CategorySelector from "@/components/CategorySelector/CategorySelector.vue";
import AddOrUpdateSpu from "./component/addOrUpdateSpu.vue";
import SaveSku from "./component/saveSku.vue";
import { getSpuListApi, removeSpuApi,getSkuListApi } from "@/api/product/spu/spu";
export default {
  name: "Spu",
  components: {
    CategorySelector,
    AddOrUpdateSpu,
    SaveSku,
  },
  data() {
    return {
      SpuList: [],
      page: 1,
      limit: 5,
      total: 15,
      category1Id: "",
      category2Id: "",
      category3Id: "",
      showList: 1,
      setSpuList: {},
      dialogVisible:false,
      SkuList:[]
    };
  },
  methods: {
    //自定义事件切换页面
    showPage(page) {
      this.showList = page;
    },
    //查看SKU列表
    async moveSku(row){
      this.dialogVisible=true
      const res = await getSkuListApi(row.id)
      this.SkuList=res.data
    },
    //更新Spu
    UpdateSpu(row) {
      this.setSpuList = row;
      this.showList = 2;
    },
    //添加SPU
    addSpuList() {
      this.setSpuList = {
        spuName: "",
        description: "",
        tmId: undefined,
        spuSaleAttrList: [],
        spuImageList: [],
      };
      this.showList = 2;
    },
    //删除Spu
    async removeSpu(row) {
      await removeSpuApi(row.id);
      this.getSupList();
    },
    changeCategory({ id, level }) {
      if (level === 1) {
        this.category1Id = id;
      } else if (level === 2) {
        this.category2Id = id;
      } else {
        this.category3Id = id;
        this.getSupList();
      }
    },
    async getSupList(page) {
      const { limit, category3Id } = this;
      const res = await getSpuListApi(
        page ? page : this.page,
        limit,
        category3Id
      );
      this.SpuList = res.data.records;
      this.total = res.data.total;
      if (page) {
        this.page = page;
      }
    },
    //更改每页条数触发
    handleSizeChange(val) {
      this.limit = val;
      this.getSupList();
      console.log(`每页 ${val} 条`);
    },
    //更改页数触发
    handleCurrentChange(val) {
      this.getSupList(val);
      console.log(`当前页: ${val}`);
    },
    //添加Sku
    addSkuList(row) {
      this.showList = 3;
      this.setSpuList = row;
    },
  },
  watch: {
    showList() {
      this.getSupList();
    },
  },
};
</script>

<style></style>
